<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
        >
          <el-form-item label="订单编号:">
            <el-input
              v-model="listQuery.outTradeNo"
              class="input-width"
              placeholder="订单编号"
            ></el-input>
          </el-form-item>
          <el-form-item label="会员名：">
            <el-input
              v-model="listQuery.memberNickname"
              class="input-width"
              placeholder="会员名"
            ></el-input>
          </el-form-item>
          <el-form-item label="开始时间：">
            <el-date-picker
              class="input-width"
              v-model="listQuery.startDate"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="请选择时间"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间：">
            <el-date-picker
              class="input-width"
              v-model="listQuery.endDate"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="请选择时间"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="订单状态：">
            <el-select
              v-model="listQuery.status"
              class="input-width"
              placeholder="全部"
              clearable
            >
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-button
            style=""
            type="primary"
            @click="handleSearchList()"
            size="small"
            >查询搜索</el-button
          >
          <el-button
            style="margin-right: 15px"
            @click="handleResetSearch()"
            size="small"
            >重置</el-button
          >
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button type="success" icon="el-icon-download" @click="exportToExcel">导出</el-button>
    </el-card>
    <div class="table-container">
      <el-table
        ref="orderTable"
        :data="list"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <el-table-column
          type="selection"
          width="60"
          align="center"
        ></el-table-column>
        <el-table-column label="编号" width="80" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="订单编号" width="180" align="center">
          <template slot-scope="scope">{{ scope.row.outTradeNo }}</template>
        </el-table-column>
        <el-table-column label="会员昵称" width="180" align="center">
          <template slot-scope="scope">{{ scope.row.memberNickname }}</template>
        </el-table-column>
        <el-table-column label="手机号" width="120" align="center">
          <template slot-scope="scope">{{ scope.row.phone }}</template>
        </el-table-column>
        <el-table-column label="推荐人" width="120" align="center">
          <template slot-scope="scope">{{ scope.row.parentNicename }}</template>
        </el-table-column>
        <el-table-column label="套餐名称" width="120" align="center">
          <template slot-scope="scope">{{ scope.row.mealName }}</template>
        </el-table-column>
        <el-table-column label="提交时间" width="180" align="center">
          <template slot-scope="scope">{{
            scope.row.createDatetime | formatCreateTime
          }}</template>
        </el-table-column>
        <el-table-column label="订单金额" width="120" align="center">
          <template slot-scope="scope">￥{{ scope.row.price }}</template>
        </el-table-column>

        <el-table-column label="订单状态" width="120" align="center">
          <template slot-scope="scope">{{
            scope.row.status | formatStatus
          }}</template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <!-- <template slot-scope="scope">
            <el-button size="mini" @click="handleViewOrder(scope.$index, scope.row)">查看订单</el-button>
            <el-button
              size="mini"
              @click="handleCloseOrder(scope.$index, scope.row)"
              v-show="scope.row.status===0"
            >关闭订单</el-button>
            <el-button
              size="mini"
              @click="handleDeliveryOrder(scope.$index, scope.row)"
              v-show="scope.row.status===1"
            >订单发货</el-button>
            <el-button
              size="mini"
              @click="handleViewLogistics(scope.$index, scope.row)"
              v-show="scope.row.status===2||scope.row.status===3"
            >订单跟踪</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDeleteOrder(scope.$index, scope.row)"
              v-show="scope.row.status===4"
            >删除订单</el-button>
          </template>-->
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[50, 100, 500]"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import { fetchList } from "@/api/ums/membermealorder";
import { formatDate } from "@/utils/date";
const defaultListQuery = {
  pageNum: 1,
  pageSize: 100,
  outTradeNo: null,
  memberNickname: null,
  status: null,
  startDate: null,
  endDate: null,
};
export default {
  name: "orderList",
  components: {},
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      listLoading: true,
      list: null,
      total: null,
      multipleSelection: [],
      statusOptions: [
        {
          label: "待付款",
          value: 0,
        },
        {
          label: "已付款",
          value: 1,
        },
      ],
      tHeader:['编号', '订单编号', '用户昵称', '手机号' , '推荐人', '套餐名称','创建时间','金额','状态'],
      tValue:['id', 'outTradeNo', 'memberNickname', 'phone', 'parentNicename', 'mealName','createDatetime','price','status'],
    };
  },
  created() {
    this.getList();
  },
  filters: {
    formatCreateTime(time) {
      let date = new Date(time);
      return formatDate(date, "yyyy-MM-dd hh:mm:ss");
    },
    formatStatus(value) {
      if (value === 0) {
        return "待付款";
      } else if (value === 1) {
        return "已付款";
      }
    },
  },
  methods: {
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleViewOrder(index, row) {
      //this.$router.push({ path: "/oms/orderDetail", query: { id: row.id } });
    },
    handleDeleteOrder(index, row) {
      let ids = [];
      ids.push(row.id);
      this.deleteOrder(ids);
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then((response) => {
        this.listLoading = false;
        this.list = response.data.records;
        this.total = response.data.total;
      });
    },
    deleteOrder(ids) {
      this.$confirm("是否要进行该删除操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let params = new URLSearchParams();
        params.append("ids", ids);
        deleteOrder(params).then((response) => {
          this.$message({
            message: "删除成功！",
            type: "success",
            duration: 1000,
          });
          this.getList();
        });
      });
    },
    exportToExcel () {
            this.$confirm('确定下载列表文件?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.export2Excel()
            }).catch(() => {})
    },
    export2Excel () {
        var that = this
        require.ensure([], () => {
            const { export_json_to_excel } = require('@/assets/js/Export2Excel.js')// js文件的位置
            const tHeader = that.tHeader;
            const filterVal = that.tValue;// 导出的表头字段名，需要导出表格字段名
            const data = that.formatJson(filterVal, that.list);
            export_json_to_excel(tHeader, data, 'VIP订单表')// 导出的表格名称，根据需要自己命名
        })
    },
    formatJson (filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => v[j]))
    }
  },
};
</script>
<style scoped>
.input-width {
  width: 203px;
}
</style>


